#ifndef TFRMSTRUCT_H
#define TFRMSTRUCT_H

#include <QWidget>
#include <QTextBrowser>
#include <QTreeWidget>
#include <QSyntaxHighlighter>
#include <QHostAddress>
#include "tlrstructmodel.h"

namespace Ui {
class TFrmStruct;
}

class TFrmStruct : public QWidget
{
    Q_OBJECT

public:
    explicit TFrmStruct(QWidget *parent = nullptr);
    ~TFrmStruct();

    //重置
    void reset(int nindex);
    //获取序号
    int getIndex();
    //读取数据
    void rdPara(const QString &nequidstr,int nparastaadd,const QHostAddress &nip,int nport,int nserialaddr);
    void rdParaSerial(const QString &nequidstr,int nparastaadd,int nserialaddr);
    void rdParaNetToSerial(const QString &equid_str,int para_sta_add,const QHostAddress &ip,int port,int serial_addr,int serial_port,int baud);
    //下发数据
    void wrPara(const QString &nequidstr,int nparastaadd,const QHostAddress &nip,int nport,int nserialaddr);
    void wrParaSerial(const QString &nequidstr,int nparastaadd,int nserialaddr);
    //控制数据
    void ctlPara(const QString &nequidstr,int nparastaadd,const QHostAddress &nip,int nport,int nserialaddr);
    void ctlParaSerial(const QString &nequidstr,int nparastaadd,int nserialaddr);
    //控制数据(单条)
    bool ctlPara_Single(const QString &equid_str,int parasta_add,const QHostAddress &ip,int port,int serial_add,QString &error_str);
    //导入/导出数据
    void importData();
    void exportData();

private:
    Ui::TFrmStruct *ui;
    enum TUIType {
        uiFrame,    //刷新界面
    };

    //结构体数据分析器
    QTreeWidget *m_treeStructMonitor = new QTreeWidget(this);
    //结构体标准格式显示
    QTextEdit *m_teStructFormat = nullptr;
    //待分析结构体序号
    int m_index = -1;
    //当前选中控制条目
    QTreeWidgetItem *m_cur_item = nullptr;

    //UI处理
    void dataToUI(const TUIType &nuitype);
    //结构体解析到树
    void decodeStructToTree(int npreoffset,const TLRStruct &nlrstruct,const QByteArray nactmemdata,QTreeWidgetItem *prootitem = nullptr);
    //树恢复到结构体
    void encodeTreeToStruct(TLRStruct &nlrstruct);
    //遍历树条目到结构体
    void queryTreeItemToStruct(const QTreeWidgetItem *ptreeitem,TLRStruct &nlrstruct);
    //遍历结构体到树条目
    void queryStructToTreeItem(QTreeWidgetItem *ptreeitem,const TLRStruct &nlrstruct);

private slots:
    //结构体数据更新
    void slotStructDataUpdate(int index);
    //分析树内容变化
    void slotTreeWidgetItemChanged(QTreeWidgetItem *item, int column);
    //当前选中条目变更
    void slotCurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);

};

#endif // TFRMSTRUCT_H
